package com.ruoyi.ysz.yfjftr.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ysz.yfjftr.domain.Yfjftr;
import com.ruoyi.ysz.yfjftr.domain.YfjftrFj;
import com.ruoyi.ysz.yfjftr.service.IYfjftrService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 研发经费投入Controller
 *
 * @author huacai
 * @date 2024-11-15
 */
@RestController
@RequestMapping("/ysz/yfjftr")
public class YfjftrController extends BaseController {
    @Resource
    private IYfjftrService yfjftrService;

    /**
     * 查询研发经费投入列表
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:list')")
    @GetMapping("/list")
    public TableDataInfo list(Yfjftr yfjftr) {
        startPage();
        List<Yfjftr> list = yfjftrService.selectYfjftrList(yfjftr);
        return getDataTable(list);
    }

    /**
     * 导出研发经费投入列表
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:export')")
    @Log(title = "研发经费投入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Yfjftr yfjftr) {
        List<Yfjftr> list = yfjftrService.selectYfjftrList(yfjftr);
        ExcelUtil<Yfjftr> util = new ExcelUtil<Yfjftr>(Yfjftr.class);
        util.exportExcel(response, list, "研发经费投入数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Yfjftr> util = new ExcelUtil<Yfjftr>(Yfjftr.class);
        util.importTemplateExcel(response, "研发经费投入数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "研发经费投入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Yfjftr> util = new ExcelUtil<Yfjftr>(Yfjftr.class);
        InputStream inputStream = file.getInputStream();
        List<Yfjftr> list = util.importExcel(inputStream);
        inputStream.close();
        int count = yfjftrService.batchInsertYfjftr(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取研发经费投入详细信息
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:query')")
    @GetMapping(value = "/{yfjftrId}")
    public AjaxResult getInfo(@PathVariable("yfjftrId") String yfjftrId) {
        return success(yfjftrService.selectYfjftrByYfjftrId(yfjftrId));
    }

    /**
     * 新增研发经费投入
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:add')")
    @Log(title = "研发经费投入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Yfjftr yfjftr) {
        yfjftr.setUserId(Math.toIntExact(getUserId())); //用户ID
        yfjftr.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        yfjftr.setCreateBy(getUsername()); //创建人
        yfjftr.setIdWhere("存在");

        BigDecimal trYfJf = yfjftr.getTrYfJf(); //投入研发经费
        BigDecimal xsSr = yfjftr.getXsSr(); //销售收入
        // 计算百分比，保留两位小数，四舍五入
        BigDecimal bz = trYfJf.divide(xsSr, 6, RoundingMode.HALF_UP)
                .multiply(BigDecimal.valueOf(100))
                .setScale(2, RoundingMode.HALF_UP);
        yfjftr.setBz(bz); //插入占销售收入的比重

        return toAjax(yfjftrService.insertYfjftr(yfjftr));
    }

    /**
     * 修改研发经费投入
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:edit')")
    @Log(title = "研发经费投入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Yfjftr yfjftr) {
        return toAjax(yfjftrService.updateYfjftr(yfjftr));
    }

    /**
     * 删除研发经费投入
     */
    @PreAuthorize("@ss.hasPermi('ysz:yfjftr:remove')")
    @Log(title = "研发经费投入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{yfjftrIds}")
    public AjaxResult remove(@PathVariable String[] yfjftrIds) {
        return toAjax(yfjftrService.deleteYfjftrByYfjftrIds(yfjftrIds));
    }

    /**
     * 获得研发经费投入主键ID (UUID)
     */
    @GetMapping("/getYfJfTrId")
    public AjaxResult getYfJfTrId() {
        String getYfJfTrId = IdUtils.fastSimpleUUID();
        return success(getYfJfTrId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertYfjftrFjByYfjftrId(@RequestBody YfjftrFj yfjftrFj) {
        return toAjax(yfjftrService.insertYfjftrFjByYfjftrId(yfjftrFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectYfjftrFjList(String sId) {
        YfjftrFj yfjftrFj = new YfjftrFj();
        yfjftrFj.setYfjftrId(sId);
        return AjaxResult.success(yfjftrService.selectYfjftrFjList(yfjftrFj));
    }

    /**
     * 删除研发经费投入附件
     */
    @Log(title = "研发经费投入附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteYfjftrFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(yfjftrService.deleteYfjftrFjByFjIds(fjIds));
    }

}
